<?php
/**
 * 用户相关接口
 * */

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Model\FormatCheck;
use App\Http\Services\UserServices;
use App\Http\Services\MsgServices;

class ApiUserController extends BaseController
{
    protected $smsPrefix = 'sms_';
    protected $service;
    protected $thirdSourceConf = [1,2,3,4,5,6,7,8,9];//1:qq 2:微信 3：微博 4:华为 5.来来微信 7.天天微信 8.火火微信 9苹果登录
    protected $platformConf = [1,2];//1:iOS 2:android
    protected $actConf = [1,2];//1:绑定 2：解绑
    public static  $expire = 7200;
    private $service_msg;

    public function __construct(UserServices $service, MsgServices $msgServices)
    {
        $this->service = $service;
        $this->service_msg = $msgServices;
    }

    /**
     * wap 公众号 手机号验证码登录
     * @param Request $request
     */
    public function mpLoginSmsCode(Request $request)
    {
        init_redis_session();

        $code = Hint::SUCCESS;
        $rest = [];

        do {
            $phone = $request->post('phone') ?? '';
            $smsCode = $request->post('sms_code') ?? '';
            if (!$phone || !FormatCheck::checkPhone($phone)) {
                $code=Hint::PHONE_FORMAT_ERR;
                break;
            }
            if (!$smsCode) {
                $code=Hint::PARAM_ERROR;
                break;
            }

            // redis 获取手机号匹配的验证码
            $checkSmsCode = $this->redisCacheGet($this->smsPrefix . $phone);
            if(empty($checkSmsCode) || ($checkSmsCode != $smsCode)){
                $code = Hint::SMS_CODE_ERROR;
                break;
            }

            // 通过用户手机号获取用户信息
            $rest = $this->service->getUserInfoByPhone($phone);
            if (empty($rest)) {
                $code = Hint::PHONE_STATUS_1;
                break;
            }

            // 增加session
            $_SESSION['user_id'] = $rest['user_id'];
        }while(false);

        Response::returnResult($code, $rest);
    }


    /**
     * wap 公众号登录
     * @param Request $request
     */
    public function mp_login(Request $request)
    {
        init_redis_session();
        $phone = $request->post('phone');
        $password = $request->post('password');
        if (!$phone || !FormatCheck::checkPhone($phone))
            Response::returnResult(Hint::PHONE_FORMAT_ERR, []);

        if (!$password)
            Response::returnResult(Hint::PARAM_ERROR, []);
        $ret = $this->service->mp_login($phone, $password);
        if (empty($ret)){
            Response::returnResult(Hint::PASSWORD_ERROR, []);
        }
        Response::returnResult(Hint::SUCCESS, $ret);
    }



    /**
     * wap 公众号登录信息
     * @param Request $request
     */
    public function mp_info(Request $request)
    {
        init_redis_session();
        $ret = $this->service->mp_info();
        if (empty($ret)){
            Response::returnResult(Hint::LOGIN_OUT, []);
        }
        Response::returnResult(Hint::SUCCESS, $ret);
    }

    //redis存储获取
    private function redisCacheGet($key){
        return Redis::get($key);
    }


    /**
     * 主播信息编辑并审核
     * */
    public function identityToVerify(Request $request)
    {
        $user = $this->service->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $voice = getUrlPath($request->post("voice"));
        $voice_time = $request->post("voice_time");
        $sign = $request->post("sign");
        if(empty($voice) || empty($voice_time) || empty($sign)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //实名认证
        if($user['active_certify'] != 1) {
            Response::returnResult(Hint::USER_NOT_VERIFY);
        }
        //待审核
        if(!empty($user['identity_status']) && $user['identity_status']==2) {
            Response::returnResult(Hint::PARAM_ERROR, [], '当前待审核,不可编辑');
        }

        $result = $this->service->identityToVerify($user['user_id'], $voice, $voice_time, $sign);

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 主播身份状态编辑
     * */
    public function identityStatusUpdate(Request $request)
    {
        $user = $this->service->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $identityStatus = (int) $request->post("identity_status");
        if(empty($identityStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //审核通过的
        if(!empty($user['identity_status']) && in_array($user['identity_status'], [1, 3])) {
            $result = $this->service->identityStatusUpdate($user['user_id'], $identityStatus);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 主播列表
     * */
    public function identityList(Request $request)
    {
        $user = $this->service->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->service->identityList($user['user_id']);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 面试官身份点击主播提醒
     * */
    public function identityClickMsg(Request $request)
    {
        $user = $this->service->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $request->input('user_id');
        if (empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $this->service_msg->identityClickMsg($user['user_id'], $userId, '用户已接入');

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 判断对方是否被我送过礼物
     * */
    public function checkUserGetGift(Request $request)
    {
        $user = $this->service->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $toUserId = $request->input('to_user_id');
        if (empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->checkUserGetGift($user['user_id'], $toUserId, '用户已接入');
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 获取用户身份
     * */
    public function getUserIdentity(Request $request)
    {
        $toUserId = $request->input('to_user_id');
        if (empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->getUserIdentity($toUserId);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 获取主播收礼排行榜
     * */
    public function getAnchorAcceptGiftRank(Request $request)
    {
        $type = $request->input('type');//1=实时榜单 2=每日榜单
        $toUserId = $request->input('to_user_id');
        if (empty($type) || empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->getAnchorAcceptGiftRank($toUserId, $type);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 删除主播收礼实时榜单
     * */
    public function delAnchorAcceptGiftRank(Request $request)
    {
        $user = $this->service->get_current_user();
        if(empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $user['user_id'];
        $toUserId = $request->input('to_user_id');
        $roomId = $request->input('room_id');
        $userIds = $request->input('user_ids', '');//删除用户ID
        $userIds = explode(',', $userIds);
        if (empty($roomId) || empty($toUserId) || empty($userIds)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->delAnchorAcceptGiftRank($userId, $toUserId, $roomId, $userIds);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 记录用户私信数据
     */
    public function saveUserMsg(Request $request)
    {
        $fromUserId = $this->getLoginUserId();
        if(empty($fromUserId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $content = $request->input("content");
        $toUserId = (int) $request->input("to_user_id");
        $roomId = (int) $request->input("room_id");
        $placeType = (int) $request->input("place_type");
        if(empty($content)) {
            Response::returnResult(Hint::SUCCESS);
        }

        $this->service->saveUserMsg($fromUserId, $content, $toUserId, $roomId, $placeType);

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 用户设置-详情
     * */
    public function getUserSet(Request $request)
    {
        $userId = (int) $request->input("user_id");
        if(empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->getUserSet($userId);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 用户设置-编辑
     * */
    public function editUserSet(Request $request)
    {
        $userId = (int) $request->input("user_id");
        $giftNotice = (int) $request->input("gift_notice");
        $noMsgFriend = (int) $request->input("no_msg_friend");
        if(empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->editUserSet($userId, $giftNotice, $noMsgFriend);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }






}
